package com.ronglian.ucdisk.web;

import java.util.HashMap;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;



public class MessageBean {

 
	private static Logger logger=LoggerFactory.getLogger(MessageBean.class);

	//private MessageHead head=new MessageHead(200,null);

	
	 public int status=200;

	 public String msg;
	    
	 public String resourceKey;
	
	
    public HashMap<String,Object> body=new HashMap<String,Object>();
    
    
    public void fireSuccess()
    {
    	this.status=MessageHead.STATUS_SUCESS;
    }
    
    public void firePermissionDeny()
    {
    	this.status=MessageHead.STATUS_PERMISSIONDENY;
    }
    
    public boolean checkSuccess()
    {
    	return this.status ==MessageHead.STATUS_SUCESS;
    }
    
    public boolean checkFailed()
    {
    	return this.status ==MessageHead.STATUS_FAILED;
    }
    
    public boolean checkException()
    {
    	return this.status ==MessageHead.STATUS_EXCEPTION;
    }
    
    
    
    
    public void fireFailed()
    {
    	this.status=MessageHead.STATUS_FAILED;
    }
    
    public void fireException()
    {
    	this.status=MessageHead.STATUS_EXCEPTION;
    }
   
    
    public void putBody(String key,Object value)
    {
    	this.body.put(key, value);
    }
    
    public Object getObject(String key,Class<?> clazz)
    {
    	Object obj=this.body.get(key);
    	if(obj==null)return null;
    	else if(obj.getClass().getName().equals(clazz.getName()))return obj;
    	else 
    	{
    		try
    		{
    			JSONObject jsonObject = JSONObject.fromObject(obj);
    			return JSONObject.toBean(jsonObject, clazz);
    		}catch(Exception e)
    		{
    			logger.error(e.getMessage());
    			return null;
    		}
    	}
    	
    }
    
    public String toJson()
    {
    	JSONObject jsonObject = JSONObject.fromObject(this);
		return jsonObject.toString();
    }
    
}
